package com.algo.rpc.server;

import com.algo.rpc.common.Constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务注册信息类
 */
public class ServiceRegistry {

    private List<ServiceConfig> serviceConfigList = new ArrayList<ServiceConfig>();

    private Map<Class, Class> serviceMapping = new HashMap<Class, Class>();

    private static final ServiceRegistry registry = new ServiceRegistry();

    // serviceConfig 扫描类
    private ServiceConfigScanner serviceConfigScanner;

    private ServiceRegistry() {
        serviceConfigScanner = new ServiceConfigScanner(Constants.SERVICE_PROVIDER_PACKAGE_NAME);
        serviceConfigList = serviceConfigScanner.doScan();
        for (ServiceConfig serviceConfig : serviceConfigList) {
            serviceMapping.put(serviceConfig.getServiceInterface(), serviceConfig.getServiceImpl());
        }
    }

    public static ServiceRegistry getInstance() {
        return registry;
    }

    /**
     * 获取所有的服务信息
     * @return
     */
    public List<ServiceConfig> getServiceConfigList() {
        return serviceConfigList;
    }

    /**
     * 获取service映射
     * @return
     */
    public Map<Class, Class> getServiceMapping() {
        return serviceMapping;
    }

}
